package com.ruoyi.master.controller;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.master.config.redis.RedisPushService;
import com.ruoyi.master.domain.TTask;
import com.ruoyi.master.domain.WeightDriftRateVO;
import com.ruoyi.master.service.ITTaskService;
import com.ruoyi.master.util.TaskStatusEnum;
import com.ruoyi.system.api.domain.WxText;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 任务Controller
 *
 * @author jiandan
 * @date 2024-06-27
 */
@RestController
@RequestMapping("/task")
public class TTaskController extends BaseController {
    @Autowired
    private ITTaskService tTaskService;

    @Autowired
    private RedisPushService redisPushService;

    /**
     * 查询任务列表
     */
//    @RequiresPermissions("master:task:list")
    @GetMapping("/list")
    public TableDataInfo list(TTask tTask){
        startPage();
        List<TTask> list = tTaskService.selectTTaskList(tTask);
        return getDataTable(list);
    }

    /**
     * 查询任务列表
     */
//    @RequiresPermissions("master:task:listByLoginUser")
    @GetMapping("/listByLoginUser")
    public TableDataInfo listByLoginUser(TTask tTask) throws Exception {
        if (0L == SecurityUtils.getUserId()) {
            return getDataTable(null);
        }
        tTask.setStatus(TaskStatusEnum.NORMAL.getValue());
        tTask.setCreateBy(Long.toString(SecurityUtils.getUserId()));
        startPage();
        List<TTask> list = tTaskService.selectTTaskList(tTask);
        return getDataTable(list);
    }

    /**
     * 查询冲浪任务列表
     */
    @GetMapping("/surfingList")
    public TableDataInfo surfingList(TTask tTask) {
        if (0L == SecurityUtils.getUserId()) {
            return getDataTable(null);
        }
        tTask.setStatus(TaskStatusEnum.NORMAL.getValue());
        startPage();
        List<TTask> list = tTaskService.surfingList(tTask);
        return getDataTable(list);
    }

    @GetMapping("/selectTodayDuration")
    public AjaxResult selectTodayDuration(TTask tTask){
        List<TTask> taskList = tTaskService.selectTodayDuration(tTask);
        return success(taskList);
    }

    @GetMapping("/totalDurationMin")
    public AjaxResult totalDurationMin() {
        return success(tTaskService.totalDurationMin());
    }


    /**
     * 导出任务列表
     */
//    @RequiresPermissions("master:task:export")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TTask tTask) {
        List<TTask> list = tTaskService.selectTTaskList(tTask);
        ExcelUtil<TTask> util = new ExcelUtil<TTask>(TTask.class);
        util.exportExcel(response, list, "任务数据");
    }

    /**
     * 获取任务详细信息
     */
//    @RequiresPermissions("master:task:query")
    @GetMapping("/getTaskAndUpsByTaskId/{taskId}")
    public AjaxResult getTaskAndUpsByTaskId(@PathVariable("taskId") Long taskId) {
        return success(tTaskService.selectTTaskByTaskContentAndCreater(taskId,null,null));
    }


    /**
     * 获取任务详细信息
     */
//    @RequiresPermissions("master:task:query")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId) {
        return success(tTaskService.selectTTaskByTaskId(taskId));
    }

    /**
     * 新增任务
     */
//    @RequiresPermissions("master:task:add")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTask tTask) {
        if (StringUtils.isNotEmpty(tTask.getTaskContent())) {
            return toAjax(tTaskService.insertTaskFromApp(tTask));
        }
        return error();
    }

    /**
     * 修改任务
     */
//    @RequiresPermissions("master:task:edit")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTask tTask) {
        return toAjax(tTaskService.updateTTask(tTask));
    }

    /**
     * 归档任务
     */
//    @RequiresPermissions("master:task:edit")
    @Log(title = "归档任务", businessType = BusinessType.UPDATE)
    @PostMapping("/filingByTaskId")
    public AjaxResult filingByTaskId(@RequestBody TTask tTask) {
        tTask.setStatus(TaskStatusEnum.FILING.getValue());
        return toAjax(tTaskService.updateTTask(tTask));
    }

    /**
     * 公开任务
     */
//    @RequiresPermissions("master:task:edit")
    @Log(title = "公开任务", businessType = BusinessType.UPDATE)
    @PostMapping("/openByTaskId")
    public AjaxResult openByTaskId(@RequestBody TTask tTask) {
        return toAjax(tTaskService.updateTTask(tTask));
    }

    /**
     * 修改任务权重
     */
//    @RequiresPermissions("master:task:edit")
    @Log(title = "修改任务权重", businessType = BusinessType.UPDATE)
    @PostMapping("/editWeightByTaskId")
    public AjaxResult editWeightByTaskId(@RequestBody TTask tTask) {
        int rs = tTaskService.editWeightByTaskId(tTask);
        AjaxResult ajaxResult = toAjax(rs);
        if (rs == -1) {
            ajaxResult.put("msg", "操作成功");
        }else{
            ajaxResult.put("msg", "总权重不能大于100，其他任务已占" + rs + "");
        }
        return ajaxResult;
    }

    /**
     * 获取权重任务偏离度
     */
    @Log(title = "获取权重任务偏离度", businessType = BusinessType.UPDATE)
    @GetMapping("/weightDriftRate/{taskId}")
    public AjaxResult weightDriftRate(@PathVariable("taskId") Long taskId) {
        List<WeightDriftRateVO> rs = tTaskService.weightDriftRate(null,taskId);
        return success(rs);
    }

    /**
     * 删除任务
     */
    @RequiresPermissions("master:task:remove")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds) {
        return toAjax(tTaskService.deleteTTaskByTaskIds(taskIds));
    }

    /**
     * 处理来自微信的任务
     */
    @InnerAuth
//    @Log(title = "来自微信的任务", businessType = BusinessType.INSERT)
    @PostMapping("/wxtask")
    public AjaxResult weixinTask(@RequestBody WxText wxText) {
        logger.info("-----处理来自微信的任务-----");
        String rs = tTaskService.insertTaskFromWechat(wxText);
        //通过消息队列处理
//        String rs = redisPushService.push(wxText);
        AjaxResult ajaxResult = toAjax(1);
        if(StringUtils.isNotEmpty(rs)){
            ajaxResult.put("msg", rs);
        }
        return ajaxResult;
    }
}
